//
//  DetailViewController.m
//  HQJFacilitator
//
//  Created by mymac on 16/9/20.
//  Copyright © 2016年 Fujian first time iot technology investment co., LTD. All rights reserved.
//

#import "DetailViewController.h"
#import "ToolView.h"
#import "DetailViewModel.h"
#import "DetailTableViewCell.h"
#import "DerailModel.h"
#import "DetailTransferTableViewCell.h"


@interface DetailViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    NSInteger _scrollOffSize;
    
}
@property (nonatomic,strong)UITableView *DetailTabbaleViewm;
@property (nonatomic,strong)UIScrollView *detailScrollView;
@property (nonatomic,strong)ToolView *hqj_ToolView;
@property (nonatomic,strong)NSMutableArray *tableViewAry;
@property (nonatomic       )NSInteger pageScroll;
@property (nonatomic,strong)DetailViewModel *viewModel;
@property (nonatomic,strong)DerailModel *model;
@property (nonatomic,strong)NSMutableArray *interactiveListArray;
@property (nonatomic,strong)NSMutableArray *cashListArray;
@property (nonatomic,strong)NSMutableArray *transferListArray;
@property (nonatomic,strong)NSMutableArray *givingListArray;
@property (nonatomic       )NSInteger pageData;
@property (nonatomic,strong)UITableView *interactiveTableView;
@property (nonatomic,strong)UITableView *cashTableView;
@property (nonatomic,strong)UITableView *transferTableView;
@property (nonatomic,strong)UITableView *givingTableView;
@end

@implementation DetailViewController
//-(UITableView *)DetailTabbaleView {
//    if (!_DetailTabbaleView) {
//       _DetailTabbaleView = [[UITableView alloc]init];
//        _DetailTabbaleView.frame = CGRectMake(0, 0, WIDTH, HEIGHT-64-44-44);
//        _DetailTabbaleView.backgroundColor = [UIColor groupTableViewBackgroundColor];
//        _DetailTabbaleView.delegate = self;
//        _DetailTabbaleView.dataSource = self;
//        [_DetailTabbaleView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableView class])];
//        
//        _DetailTabbaleView.tableFooterView = [UIView new];
//       
//    }
//    
//    
//    
//    return _DetailTabbaleView;
//}
-(NSArray *)titleArray {
   NSArray *array =@[@"互动",@"兑现",@"转移",@"赠送"];
    return array;
}
-(UIScrollView *)detailScrollView {
    if (!_detailScrollView) {
        _detailScrollView =[[UIScrollView alloc]init];
        _detailScrollView.frame = CGRectMake(0, 44, WIDTH, HEIGHT-44);
        _detailScrollView.alwaysBounceHorizontal=YES;
        _detailScrollView.bounces = YES;
        _detailScrollView.pagingEnabled = YES;
        _detailScrollView.delegate = self;
        _detailScrollView.userInteractionEnabled = YES;
        _detailScrollView.showsHorizontalScrollIndicator = NO;
        _detailScrollView.showsVerticalScrollIndicator = NO;
        _detailScrollView.contentOffset = CGPointMake(0, 0);
        _detailScrollView.contentSize = CGSizeMake(WIDTH*[self titleArray].count, HEIGHT-64-44-44);
        [_detailScrollView scrollRectToVisible:CGRectMake(0,0,WIDTH,HEIGHT-64-44-44) animated:NO];
        

    }
    return _detailScrollView;
}
-(void)initTableView {
    for (NSInteger i=0; i<[self titleArray].count; i++) {
        
        UITableView* DetailTabbaleView = [[UITableView alloc]init];
        
        DetailTabbaleView.tag = i;
        
        DetailTabbaleView.frame = CGRectMake(i%[self titleArray].count*WIDTH, 0, WIDTH, HEIGHT-64-44-44);
        DetailTabbaleView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        DetailTabbaleView.delegate = self;
        DetailTabbaleView.dataSource = self;
        [DetailTabbaleView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableView class])];
        [DetailTabbaleView registerClass:[DetailTableViewCell class] forCellReuseIdentifier:NSStringFromClass([DetailTableViewCell class])];
        [DetailTabbaleView registerClass:[DetailTransferTableViewCell class] forCellReuseIdentifier:NSStringFromClass([DetailTransferTableViewCell class])];
        DetailTabbaleView.tableFooterView = [UIView new];
        DetailTabbaleView.rowHeight = 70.f;
        [self.detailScrollView addSubview:DetailTabbaleView];
        DetailTabbaleView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            HQJLog(@"刷新了");
            [self requstDetail];
        }];

       

        DetailTabbaleView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.pageData ++;
            [self requstDetail];

        }];
        switch (DetailTabbaleView.tag) {
            case 0:
                _interactiveTableView = DetailTabbaleView;
                break;
            case 1:
                _cashTableView = DetailTabbaleView;
                break;
            case 2:
                _transferTableView = DetailTabbaleView;
                break;
            case 3:
                _givingTableView = DetailTabbaleView;
                break;
            default:
                break;
        }
        
    [_tableViewAry addObject:DetailTabbaleView];
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tableViewAry = [NSMutableArray array];
    
    self.pageScroll = 0;
    
    self.pageData = 1;
    
//    [self beginRefreshing];
    
    _viewModel = [[DetailViewModel alloc]init];
    
    _interactiveListArray = [NSMutableArray array];
    _cashListArray = [NSMutableArray array];
    _transferListArray = [NSMutableArray array];
    _givingListArray = [NSMutableArray array];
    _scrollOffSize = 0;
    
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (iOS7) {
        self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;
        
    }
    
    [self SignlaRac];
//    self.view.backgroundColor = [UIColor greenColor];
    _hqj_ToolView = [[ToolView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 44) andTitleAry:[self titleArray]];
    
    @weakify(self);
    [_hqj_ToolView setToolBlock:^(UIButton *btn) {
        self_weak_.pageData = 1;

    self_weak_.detailScrollView.contentOffset = CGPointMake(WIDTH *btn.tag, 0)  ;
        self_weak_.pageScroll = btn.tag;
//        self_weak_.listArray = [NSMutableArray array];
    }];
    [self.view addSubview:self.detailScrollView];
    [self initTableView];
   
//    [self.detailScrollView addSubview:self.DetailTabbaleView];

    [self.view addSubview:_hqj_ToolView];

}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   

    
    if (tableView.tag == 0) {
        return self.interactiveListArray.count;

    } else if (tableView.tag == 1) {
        return self.cashListArray.count;

        
    } else if (tableView.tag == 2) {
        
        return self.transferListArray.count;

        
    }else {
        
        return self.givingListArray.count;

        
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (tableView.tag == 0) {
        DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DetailTableViewCell class]) forIndexPath:indexPath];
        cell.TitleImageView.image = [UIImage imageNamed:@"Deintegral"];
        cell.isCashList = NO;
        HQJLog(@"--互动界面的数据:--%@",self.interactiveListArray);
        cell.cellModel = self.interactiveListArray[indexPath.row];
        [cell updateLayout];
        return cell;
        
        
        
    } else if (tableView.tag == 1) {
        DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DetailTableViewCell class]) forIndexPath:indexPath];
        cell.TitleImageView.image = [UIImage imageNamed:@"shangjia"];
        cell.isCashList = YES;
//        HQJLog(@"--兑现界面的数据:--%@",self.interactiveListArray);

//        cell.cellModel = self.interactiveListArray[indexPath.row];
        cell.cellModel = self.cashListArray[indexPath.row];
        [cell updateLayout];
        return cell;
    } else if (tableView.tag == 2) {
        DetailTransferTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DetailTransferTableViewCell class]) forIndexPath:indexPath];
        HQJLog(@"--转移界面的数据:--%@",self.transferListArray);
//        cell.cellModel = self.interactiveListArray[indexPath.row];

        cell.cellModel =self.transferListArray[indexPath.row];
        [cell updateLayout];
        
        
        
        return cell;

    } else {
        
        DetailTransferTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DetailTransferTableViewCell class]) forIndexPath:indexPath];
        HQJLog(@"--赠送界面的数据:--%@",self.givingListArray);
//        cell.cellModel = self.interactiveListArray[indexPath.row];

        cell.cellModel =self.givingListArray[indexPath.row];
        [cell updateLayout];
        
        
        
        return cell;

    }
    
    
    
    
    
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    
    if (scrollView == self.detailScrollView) {
//        HQJLog(@"-偏移量----%f",scrollView.contentOffset.x);
        //    _hqj_ToolView ClickBtn:<#(NSInteger)#>
       self.pageScroll = scrollView.contentOffset.x/WIDTH;
        [_hqj_ToolView ClickBtn :self.pageScroll];
        self.pageData = 1;
    }
}
-(void)requstDetail {
    
   
    
    @weakify(self);
//
    [_viewModel setRequstBlock:^(NSMutableArray *returnDetailBlock) {
        
        if (self_weak_.pageData != 1){
            if (self_weak_.pageScroll == 0) {
                            [self_weak_.interactiveListArray addObjectsFromArray:returnDetailBlock];
            } else if (self_weak_.pageScroll == 1){
                
                            [self_weak_.cashListArray addObjectsFromArray:returnDetailBlock];
                
            } else if (self_weak_.pageScroll == 2) {
                            [self_weak_.transferListArray addObjectsFromArray:returnDetailBlock];
                
            } else {
                            [self_weak_.givingListArray addObjectsFromArray:returnDetailBlock];
                
            }

            
        } else {
            if (self_weak_.pageScroll == 0) {
                self_weak_.interactiveListArray =(NSMutableArray *)returnDetailBlock;
                
            } else if (self_weak_.pageScroll == 1){
                self_weak_.cashListArray =(NSMutableArray *)returnDetailBlock;
                
                
            } else if (self_weak_.pageScroll == 2) {
                self_weak_.transferListArray =(NSMutableArray *)returnDetailBlock;
                
            } else {
                self_weak_.givingListArray =(NSMutableArray *)returnDetailBlock;
                
            }
        }
        if (returnDetailBlock.count == 0){
            if(self_weak_.pageData > 1 ) {
                self_weak_.pageData --;

            }
           
//            [self_weak_ stopRefreshing];
        }
            
        [self_weak_ stopRefreshing];

    }];
    [_viewModel requstAppOrgTyp:self.pageScroll pag:self.pageData andShowView:self.view];
    
    
}


-(void)stopRefreshing {
    
    //-- 刷新表
    [_givingTableView reloadData];
    [_interactiveTableView reloadData];
    [_cashTableView reloadData];
    [_transferTableView reloadData];
    // -- 首
    [_givingTableView.mj_header endRefreshing];
    [_interactiveTableView.mj_header endRefreshing];
    [_cashTableView.mj_header endRefreshing];
    [_transferTableView.mj_header endRefreshing];
    
    // -- 尾
    [_givingTableView.mj_footer endRefreshing];
    [_interactiveTableView.mj_footer endRefreshing];
    [_cashTableView.mj_footer endRefreshing];
    [_transferTableView.mj_footer endRefreshing];
    
}

-(void)SignlaRac {
    
    [RACObserve(self, pageScroll)subscribeNext:^(id x) {
        NSLog(@"我是RAC 监听%@",x);

        if (_scrollOffSize != [x integerValue]) {
            if ([x integerValue] == 0) {
                if (_interactiveListArray.count == 0) {
                    [self requstDetail];

                }

            }else if ([x integerValue] == 1) {
                if (_cashListArray.count == 0) {
                    [self requstDetail];
                    
                }
            } else if ( [x integerValue] == 2){
                if (_transferListArray.count == 0) {
                    [self requstDetail];
                    
                }
            }else {
                if (_givingListArray.count == 0) {
                    [self requstDetail];
                    
                }
            }
           
            _scrollOffSize = [x integerValue];
        } else {
            if (_scrollOffSize == 0) {
                [self requstDetail];

            }
            
        }
        
    }];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
